package com.zzyy.study.service;

import com.zzyy.study.dao.OrderDao;
import com.zzyy.study.dao.ProductDao;
import com.zzyy.study.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private ProductService productService;

    public void submitOrder(){
        //生成订单
        Order order = new Order();
        long number = Math.abs(new Random().nextInt(500));
        order.setId(number);
        order.setOrderNo("order_" + number);
        orderDao.saveOrder(order);

        //减库存
        //productService.updateProductStockCountById(1, 1L);

        //减库存,场景07独有
        updateProductStockCountById(1, 1L);

        //只有场景6、7才打开，平时注释掉
        int age = 10/0;
    }

    //场景7才用，平时注释掉。
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateProductStockCountById(Integer stockCount, Long id){
        productDao.updateProductStockCountById(stockCount, id);
    }
}
